package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.tb.lmt.domain.vo.TrackLengthVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.TbCarparkingInformationBo;
import com.ruoyi.tb.lmt.domain.vo.TbCarparkingInformationVo;
import com.ruoyi.tb.lmt.domain.TbCarparkingInformation;
import com.ruoyi.tb.lmt.mapper.TbCarparkingInformationMapper;
import com.ruoyi.tb.lmt.service.ITbCarparkingInformationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 停车场轨道信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-21
 */
@RequiredArgsConstructor
@Service
public class TbCarparkingInformationServiceImpl implements ITbCarparkingInformationService {

    private final TbCarparkingInformationMapper baseMapper;

    @Autowired
    private TbCarparkingInformationMapper tbCarparkingInformationMapper;

    /**
     * 查询停车场轨道信息
     */
    @Override
    public TbCarparkingInformationVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询停车场轨道信息列表
     */
    @Override
    public TableDataInfo<TbCarparkingInformationVo> queryPageList(TbCarparkingInformationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbCarparkingInformation> lqw = buildQueryWrapper(bo);
        Page<TbCarparkingInformationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询停车场轨道信息列表
     */
    @Override
    public List<TbCarparkingInformationVo> queryList(TbCarparkingInformationBo bo) {
        LambdaQueryWrapper<TbCarparkingInformation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbCarparkingInformation> buildQueryWrapper(TbCarparkingInformationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbCarparkingInformation> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPlaceId() != null, TbCarparkingInformation::getPlaceId, bo.getPlaceId());
        lqw.eq(bo.getRequireId() != null, TbCarparkingInformation::getRequireId, bo.getRequireId());
        lqw.like(StringUtils.isNotBlank(bo.getTrackName()), TbCarparkingInformation::getTrackName, bo.getTrackName());
        lqw.eq(StringUtils.isNotBlank(bo.getTrackLength()), TbCarparkingInformation::getTrackLength, bo.getTrackLength());
        lqw.eq(StringUtils.isNotBlank(bo.getDrivewayNumber()), TbCarparkingInformation::getDrivewayNumber, bo.getDrivewayNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getTrackNeed()), TbCarparkingInformation::getTrackNeed, bo.getTrackNeed());
        lqw.eq(StringUtils.isNotBlank(bo.getTrackNeed2()), TbCarparkingInformation::getTrackNeed2, bo.getTrackNeed2());
        return lqw;
    }

    /**
     * 新增停车场轨道信息
     */
    @Override
    public Boolean insertByBo(TbCarparkingInformationBo bo) {
        TbCarparkingInformation add = BeanUtil.toBean(bo, TbCarparkingInformation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改停车场轨道信息
     */
    @Override
    public Boolean updateByBo(TbCarparkingInformationBo bo) {
        TbCarparkingInformation update = BeanUtil.toBean(bo, TbCarparkingInformation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbCarparkingInformation entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除停车场轨道信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<TrackLengthVo> getTrackLength() {

        return tbCarparkingInformationMapper.selectTrackLength();
    }


}
